package com.wcy.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.nacos.common.util.UuidUtils;
import com.aliyun.oss.OSS;
import com.wcy.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>Title:FileController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/25 17:09</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@Api(tags = "文件上传的控制器")
public class FileController {

    @Autowired
    private OSS ossClient; // spring-cloud-starter-alicloud-oss 自动注入该对象 报红正常

    @Value("${oss.bucket.name:coin-exchange-wangcy-imgs}")
    private String bucketName;

    @Value("${spring.cloud.alicloud.oss.endPoint:oss-cn-beijing.aliyuncs.com}")
    private String endPoint;

    @ApiOperation(value = "上传文件")
    @PostMapping("/image/AliYunImgUpload")
    @ApiImplicitParam(name = "file",value = "你要上传的文件")
    public R<String> fileUpload(MultipartFile file) throws IOException {
        /*
        *  1 bucketName
        *  2 文件名称 日期+uuid
        *  3 文件的输入流
        * */
        String fileName= DateUtil.today().replaceAll("-","/");
        String uuid = UuidUtils.generateUuid();
        fileName=fileName+"/"+uuid;
        ossClient.putObject(bucketName,fileName,file.getInputStream());
        // https://bucketName.endPoint/2021/09/25/uuid
        return R.ok("https://"+bucketName+"."+endPoint+"/"+fileName);
    }
}
